package com.plian.system.service.glcRq.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.plian.Tools.CollectionUtil;
import com.plian.system.entity.glc.GlcTaskIndicatorsRq;
import com.plian.system.entity.pf.AppraisalIndex;
import com.plian.system.mapper.glc.GlcTaskIndicatorsRqMapper;
import com.plian.system.mybatis.base.BaseServiceImpl;
import com.plian.system.service.glcRq.IGlcTaskIndicatorsRqService;
import com.plian.system.service.pf.IAppraisalIndexService;
import com.plian.system.vo.glc.GlcTaskIndicatorsRqVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: Take-off
 * @Description: TODO 管理层任期经营业绩考核指标表-任务绩效 服务实现类
 * @Date: 2022/12/14 9:29 AM
 * @Version: 1.0
 **/
@Service
@AllArgsConstructor
public class GlcTaskIndicatorsRqServiceImpl extends BaseServiceImpl<GlcTaskIndicatorsRqMapper, GlcTaskIndicatorsRq> implements IGlcTaskIndicatorsRqService {

    private IAppraisalIndexService appraisalIndexService;

    @Override
    public boolean deleteByParentId(List<Long> ids) {
        LambdaQueryWrapper<GlcTaskIndicatorsRq> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(GlcTaskIndicatorsRq::getBaseBusinessIndicatorsId, ids);
        List<GlcTaskIndicatorsRq> glcTaskIndicators = list(queryWrapper);
        if (CollectionUtil.isNotEmpty(glcTaskIndicators)){
            return deleteLogic(glcTaskIndicators.stream().map(GlcTaskIndicatorsRq::getId).collect(Collectors.toList()));
        }
        return true;
    }

    @Override
    public void setTaskName(List<GlcTaskIndicatorsRqVO> glctTaskIndicatorsRqVOS){
        List<AppraisalIndex> list = appraisalIndexService.list(new LambdaQueryWrapper<AppraisalIndex>()
                .in(AppraisalIndex::getId, glctTaskIndicatorsRqVOS.stream().map(GlcTaskIndicatorsRqVO::getIndicatorsCategoryId).collect(Collectors.toList())));
        if (CollectionUtil.isNotEmpty(list)){
            glctTaskIndicatorsRqVOS.forEach(m -> {
                List<AppraisalIndex> indices = list.stream().filter(index -> m.getIndicatorsCategoryId().equals(index.getId())).collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(indices)){
                    m.setIndicatorsCategoryName(indices.get(0).getName());
                }
            });
        }
    }
}
